package com.liesp.hrm.dao.impl;

import java.util.Hashtable;
import java.util.List;

import com.liesp.hrm.bean.CartProduct;
import com.liesp.hrm.bean.Product;
import com.liesp.hrm.dao.ICartProductDAO;

public class CartProductDAOImpl implements ICartProductDAO {

	private Hashtable hash=new Hashtable();
	public synchronized void addToCart(List<Product> vec, String id) throws NullPointerException{
		if (hash.get(id)!=null){
            CartProduct tempcp=(CartProduct)hash.get(id);
            CartProduct cp=new CartProduct();
            cp.setProductId(tempcp.getProductId());
            cp.setProductName(tempcp.getProductName());
            cp.setSelectedCount((tempcp.getSelectedCount())+1);
            cp.setProductPrice(tempcp.getProductPrice());
            hash.remove(id);
            hash.put(id,cp);
        }else{
            this.productNotExit(vec,id);
        }

	}

	public synchronized void clearCart(){
		this.hash.clear();
	}

	public synchronized void deleteFromCart(String id){
		this.hash.remove(id);
	}

	public Hashtable getCartContent(){
		 return this.hash;
	}

	public synchronized boolean isEmpty(){
		boolean empty=false;
        if(this.hash.isEmpty()){
            empty=true;
        }else{
            empty=false;
        }
        return empty;
	}

	public synchronized void productNotExit(List<Product> vec, String id) throws NullPointerException{
        int tempid=Integer.parseInt(id);
        int index = 0;
        while(!vec.isEmpty()){
            Product pb=vec.get(index);
            if(tempid==(pb.getProductId())){
                CartProduct cp=new CartProduct();
                cp.setProductId(pb.getProductId());
                cp.setProductName(pb.getProductName());
                cp.setSelectedCount(1);
                cp.setProductPrice(pb.getProductPrice());
                hash.put(id,cp);
                return ;
            }
            index++;
        }
	}

}
